package org.enate.bloatterrorist;

import java.util.ArrayList;

import org.enate.bloatterrorist.widget.PagerHeader;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

public final class PagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, PagerHeader.OnHeaderClickListener {
	private static final String TAG = "PagerAdapter";
	private final Context mContext;
	protected static ViewPager mPager;
	protected static PagerAdapter pagerAdapter;
	private final PagerHeader mHeader;
	private final static ArrayList<PageInfo> mPages = new ArrayList<PageInfo>();
	
	protected static final void buildPA() {
		mPager = (ViewPager)Main.m.findViewById(R.id.pager);
        mPager.setPageMargin(Main.m.getResources().getDimensionPixelSize(R.dimen.page_margin));
        PagerHeader pagerHeader = (PagerHeader) Main.m.findViewById(R.id.pager_header);
        pagerAdapter = new PagerAdapter(Main.m, mPager, pagerHeader);
        pagerAdapter.addPage(FilterFragment.class, "App Filter");
        pagerAdapter.addPage(AppListFragment.class, "App Overview");
        pagerAdapter.addPage(PrefsFragment.class, "Preferences");
        pagerAdapter.addPage(EventsFragment.class, "Event Manager");
        pagerAdapter.addPage(HelpFragment.class, "Help");
        mPager.setBackgroundColor(Main.prefs.getInt(API.str_pref_backgroundColor, Color.BLACK));
        mPager.setCurrentItem(1);   
	}
	
	protected static final class PageInfo {
		private final Class<?> clss;
		private final Bundle args;
		PageInfo(Class<?> _clss, Bundle _args) {clss = _clss; args = _args;}
	}

	protected PagerAdapter(FragmentActivity activity, ViewPager pager, PagerHeader header) {
		super(activity.getSupportFragmentManager());
		mContext = activity;
		mPager = pager;
		mHeader = header;
		mHeader.setOnHeaderClickListener(this);
		mPager.setAdapter(this);
		mPager.setOnPageChangeListener(this);
	}
	protected final void addPage(Class<?> clss, String title) {addPage(clss, null, title);}
	protected final void addPage(Class<?> clss, Bundle args, String title) {
		PageInfo info = new PageInfo(clss, args);
		mPages.add(info);
		mHeader.add(0, title);
		notifyDataSetChanged();
	}
	@Override public int getCount() {return mPages.size();}
	
	@Override public Fragment getItem(int position) {
		PageInfo info = mPages.get(position);
		return Fragment.instantiate(mContext, info.clss.getName(), info.args);
	}
	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		mHeader.setPosition(position, positionOffset, positionOffsetPixels);
	}
	@Override
	public void onPageSelected(int position) {
		mHeader.setDisplayedPage(position);
		switch(position) {
		case 0: 
			if(Main.prefs.getInt(API.str_pref_filterpref_ime, 0) == 0) {toggleIMM(1);} 
		break;
		default: toggleIMM(0); break;
		};
	}
	
	private static void toggleIMM(int i){
		if(1==i){
			try{
				FilterFragment.et.requestFocus();
				Main.imm.showSoftInput(FilterFragment.et, 0);
			}catch(Exception e){}
		}
		else{
			try{Main.imm.hideSoftInputFromWindow(FilterFragment.et.getWindowToken(), 0);} catch(Exception e){}
		}
	}
	
	@Override public void onPageScrollStateChanged(int state) {}
	@Override public void onHeaderClicked(int position) {}
	@Override public void onHeaderSelected(int position) {mPager.setCurrentItem(position);}
	@Override public void destroyItem(View view, int arg1, Object object) {}
	
}
